package com.direw.monitor;

import org.apache.catalina.connector.RequestFacade;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author DireW
 */
@SpringBootApplication
@RestController
public class MonitorApplication {
    public static void main(String[] args) {
        System.setProperty("http.agent", "st");
        SpringApplication.run(MonitorApplication.class, args);
    }

    @GetMapping("/test")
    public Map<String, String> test(ServletRequest servletRequest, ServletResponse servletResponse) {
        Map<String, String> rlt = new HashMap<>();
        String header = ((RequestFacade) servletRequest).getHeader("user-agent");
        rlt.put("current user agent", header);
        System.out.println("有人调用test方法，user-agent信息是：" + header);
        return rlt;
    }

    @GetMapping("/inner")
    public Map<String, String> innerFunction(ServletRequest servletRequest, ServletResponse servletResponse) {
        Map<String, String> rlt = new HashMap<>();
        String header = ((RequestFacade) servletRequest).getHeader("user-agent");
        rlt.put("not for others", "这个是内部方法");
        rlt.put("current user agent", header);
        System.out.println("有人调用inner方法，user-agent信息是：" + header);
        return rlt;
    }
}
